/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package daoimp;

import dao.MenuDao;
import hibernate.HibernateUtil;
import java.util.ArrayList;
import modelo.dominio.Menu;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Pablo
 */
public class MenuDaoImp implements MenuDao {

    @Override
    public void insert(Menu menu) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(menu);
        session.getTransaction().commit();
        session.close();
        
    }

    @Override
    public void delete(Menu menu) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.delete(menu);
        session.getTransaction().commit();
        session.close();
    }

    @Override
    public void update(Menu menu) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.update(menu);
        session.getTransaction().commit();
        session.close();
    }

    @Override
    public Menu getMenu(Integer id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Menu.class);
        criteria.add(Restrictions.eq("id", id));
        Menu menu = (Menu) criteria.list().get(0);
        session.close();
        return menu;
    }

    @Override
    public ArrayList<Menu> getAll() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Menu.class);
        criteria.addOrder(Order.asc("id"));
        ArrayList<Menu> list = (ArrayList<Menu>) criteria.list();
        session.close();
        return list;
    }
}
